<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form class="tabelForm" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务单号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入任务单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="发货单号" prop="shipOrderCode">
        <el-input v-model="queryParams.shipOrderCode" placeholder="请输入发货单号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable size="small">-->
<!--          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PICK_TASK_STATUS)"-->
<!--                     :key="dict.value" :label="dict.label" :value="dict.value"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PICK_TASK_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="仓库分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择仓库分类" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WAREHOUSE_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="分派给" prop="assignUserId">
        <el-select v-model="queryParams.assignUserId" placeholder="请选择分拍给谁" clearable size="small">
          <el-option v-for="user in userList" :key="user.id" :label="user.nickname"
                     :value="user.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="拣货开始时间" prop="pickStartTime">
        <el-date-picker v-model="queryParams.pickStartTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="拣货完成时间" prop="pickCompleteTime">
        <el-date-picker v-model="queryParams.pickCompleteTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <div class="toolbarBox">
      <div class="left-toolbar">
        <!-- <el-button
          type="primary"
          icon="el-icon-plus"
          size="medium"
          @click="openForm(undefined)"
          v-hasPermi="['hs:pick-task:create']"
        >新增
        </el-button> -->
        <el-button
          type="default"
          icon="el-icon-download"
          size="medium"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['hs:pick-task:export']"
        >导出
        </el-button>
        <div class="toolsbtn">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>

      </div>
      <div class="right-toolbar">
        <!-- 分页组件 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          layout="prev, pager, next"
          :background="false"
        />
      </div>
    </div>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="任务单号" align="center" prop="code"/>
      <el-table-column label="发货单号" align="center" prop="shipOrderCode"/>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.PICK_TASK_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="仓库分类" align="center" prop="type" width="100">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.WAREHOUSE_TYPE" :value="scope.row.warehouseType"/>
        </template>
      </el-table-column>
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.PICK_TASK_TYPE" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="执行人 " align="center" prop="assignUserName"/>
      <el-table-column label="拣货开始时间" align="center" prop="pickStartTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.pickStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拣货完成时间" align="center" prop="pickCompleteTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.pickCompleteTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"-->
<!--                     v-hasPermi="['hs:pick-task:update']">修改-->
<!--          </el-button>-->
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
<!--                     v-hasPermi="['hs:pick-task:delete']">删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框(添加 / 修改) -->
    <PickTaskForm ref="formRef" @success="getList"/>
  </div>
</template>

<script>
import * as PickTaskApi from '@/api/hs/picktask';
import PickTaskForm from './PickTaskForm.vue';
import {DICT_TYPE} from "@/utils/dict";
import store from "@/store";
import {listSimpleUsers} from "@/api/system/user";

export default {
  name: "PickTask",
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    }
  },
  components: {
    PickTaskForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拣货任务列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        code: null,
        shipOrderId: null,
        shipOrderCode: null,
        status: [],
        taskType: null,
        assignUserId: null,
        assignUserName: null,
        pickStartTime: [],
        pickCompleteTime: [],
        createTime: [],
      },
      userList: [],
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        this.queryParams.status = ["PICK_OVER"];
        const res = await PickTaskApi.getPickTaskPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除拣货任务编号为"' + id + '"的数据项?')
      try {
        await PickTaskApi.deletePickTask(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有拣货任务数据项?');
      try {
        this.exportLoading = true;
        const data = await PickTaskApi.exportPickTaskExcel(this.queryParams);
        this.$download.excel(data, '拣货任务.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
    /** 查询当前登录人所在仓库的用户列表 */
    async getUserList() {
      let query = {
        warehouseId: store.getters.warehouseId,
      };
      listSimpleUsers(query).then(res => {
        this.userList = res.data;
      })
    },
  }
};
</script>
